{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "3xW4O0zuKfWD"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "from openai import OpenAI\n",
        "import json\n",
        "from pydantic import BaseModel\n",
        "from typing import List\n",
        "import rich\n",
        "from google.colab import userdata"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "api_key = userdata.get('OPENAI_API_KEY')\n",
        "MODEL = \"gpt-4o-mini\"\n",
        "\n",
        "openai = OpenAI(api_key=api_key)"
      ],
      "metadata": {
        "id": "vUydnLeLKs03"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Passing json schema or Pydantic object in API call to enforce structured output**"
      ],
      "metadata": {
        "id": "blCx6gu1Vl46"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Chat Completion API\n",
        "\n",
        "https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat"
      ],
      "metadata": {
        "id": "5wlqKfVBKyjD"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "To enforce structued output we will use `parse` function of API like this `openai.beta.chat.completions.parse`, and pass `response_format` property to send json schema"
      ],
      "metadata": {
        "id": "EgXT84U9Tfqr"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "response = openai.beta.chat.completions.parse(\n",
        "    model=MODEL,\n",
        "    messages=[\n",
        "        {\"role\": \"developer\", \"content\": \"Extract the event information. \"},\n",
        "        {\"role\": \"user\", \"content\": \"Alice and Bob are going to a science fair on Friday.\"},\n",
        "    ],\n",
        "    # response_format property is different in chat and responses api\n",
        "    response_format={\n",
        "        # schema format is also bit different in chat and responses api\n",
        "        \"type\": \"json_schema\",\n",
        "        \"json_schema\": {\n",
        "            \"name\": \"calendar_event\",\n",
        "            \"schema\": {\n",
        "                \"type\": \"object\",\n",
        "                \"properties\": {\n",
        "                    \"name\": { \"type\": \"string\"},\n",
        "                    \"date\": { \"type\": \"string\" },\n",
        "                    \"participants\": { \"type\": \"array\", \"items\": { \"type\": \"string\" }},\n",
        "                },\n",
        "                \"required\": [\"name\", \"date\", \"participants\"],\n",
        "                \"additionalProperties\": False\n",
        "            },\n",
        "            \"strict\": True\n",
        "        }\n",
        "    }\n",
        ")\n",
        "print(response.choices[0].message.content)\n",
        "dictionary = json.loads(response.choices[0].message.content)\n",
        "rich.print(dictionary)\n",
        "print(dictionary[\"name\"])\n",
        "\n",
        "\n",
        "print()\n",
        "# This will return None, but if we provide pydantic's BaseModel object in API call\n",
        "# then we will get parsed object. See example below\n",
        "print(response.choices[0].message.parsed)"
      ],
      "metadata": {
        "id": "dBrUkMCUKz0s",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 102
        },
        "outputId": "ba53bc3e-e104-4af3-f3ac-682a2d73067e"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{\"name\":\"Science Fair\",\"date\":\"Friday\",\"participants\":[\"Alice\",\"Bob\"]}\n"
          ]
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "\u001b[1m{\u001b[0m\u001b[32m'name'\u001b[0m: \u001b[32m'Science Fair'\u001b[0m, \u001b[32m'date'\u001b[0m: \u001b[32m'Friday'\u001b[0m, \u001b[32m'participants'\u001b[0m: \u001b[1m[\u001b[0m\u001b[32m'Alice'\u001b[0m, \u001b[32m'Bob'\u001b[0m\u001b[1m]\u001b[0m\u001b[1m}\u001b[0m\n"
            ],
            "text/html": [
              "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\">{</span><span style=\"color: #008000; text-decoration-color: #008000\">'name'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">'Science Fair'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'date'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">'Friday'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'participants'</span>: <span style=\"font-weight: bold\">[</span><span style=\"color: #008000; text-decoration-color: #008000\">'Alice'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'Bob'</span><span style=\"font-weight: bold\">]}</span>\n",
              "</pre>\n"
            ]
          },
          "metadata": {}
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Science Fair\n",
            "\n",
            "None\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "A similar example, except for the user input."
      ],
      "metadata": {
        "id": "zbbl6F-1Sae2"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "response = openai.beta.chat.completions.parse(\n",
        "    model=MODEL,\n",
        "    messages=[\n",
        "        {\"role\": \"developer\", \"content\": \"Extract the event information. \"},\n",
        "        {\"role\": \"user\", \"content\": \"Leonardo, Ivan and Alex will be joining Taylor for dinner on Tuesday night.\"},\n",
        "    ],\n",
        "    response_format={\n",
        "        \"type\": \"json_schema\",\n",
        "        \"json_schema\": {\n",
        "            \"name\": \"calendar_event\",\n",
        "            \"schema\": {\n",
        "                \"type\": \"object\",\n",
        "                \"properties\": {\n",
        "                    \"name\": { \"type\": \"string\"},\n",
        "                    \"date\": { \"type\": \"string\" },\n",
        "                    \"participants\": { \"type\": \"array\", \"items\": { \"type\": \"string\" }},\n",
        "                },\n",
        "                \"required\": [\"name\", \"date\", \"participants\"],\n",
        "                \"additionalProperties\": False\n",
        "            },\n",
        "            \"strict\": True\n",
        "        }\n",
        "    }\n",
        ")\n",
        "print(response.choices[0].message.content)\n",
        "dictionary = json.loads(response.choices[0].message.content)\n",
        "print(dictionary[\"name\"])"
      ],
      "metadata": {
        "id": "WItchxDLK2x6",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "76f95964-c699-4236-ad1c-f9963fb3b545"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{\"name\":\"Dinner with Taylor\",\"date\":\"Tuesday\",\"participants\":[\"Leonardo\",\"Ivan\",\"Alex\",\"Taylor\"]}\n",
            "Dinner with Taylor\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Defining object schemas using Pydantic"
      ],
      "metadata": {
        "id": "D8rAuv08V4K0"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Note: `response.choices[0].message.parsed` directly returns object"
      ],
      "metadata": {
        "id": "Djx7kMsjTtn_"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "class CalendarEvent(BaseModel):\n",
        "    name: str\n",
        "    date: str\n",
        "    participants: list[str]\n",
        "\n",
        "response = openai.beta.chat.completions.parse(\n",
        "    model=MODEL,\n",
        "    messages=[\n",
        "        {\"role\": \"developer\", \"content\": \"Extract the event information. \"},\n",
        "        {\"role\": \"user\", \"content\": \"Leonardo, Ivan and Alex will be joining Taylor for dinner on Tuesday night.\"},\n",
        "    ],\n",
        "    response_format=CalendarEvent\n",
        ")\n",
        "# We have json string which we can convert into json object\n",
        "print(response.choices[0].message.content)\n",
        "dictionary = json.loads(response.choices[0].message.content)\n",
        "print(dictionary[\"name\"])\n",
        "\n",
        "print()\n",
        "# Also 'response.choices[0].message' and 'parsed' property that will return parsed object\n",
        "print(response.choices[0].message.parsed)\n",
        "rich.print(response.choices[0].message.parsed)\n",
        "obj = response.choices[0].message.parsed\n",
        "print(obj.name)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 120
        },
        "id": "BY42-QT9W_2D",
        "outputId": "d54c2c80-9507-4103-ebd1-201b4d060f03"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{\"name\":\"Dinner with Taylor\",\"date\":\"Tuesday\",\"participants\":[\"Leonardo\",\"Ivan\",\"Alex\",\"Taylor\"]}\n",
            "Dinner with Taylor\n",
            "\n",
            "name='Dinner with Taylor' date='Tuesday' participants=['Leonardo', 'Ivan', 'Alex', 'Taylor']\n"
          ]
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "\u001b[1;35mCalendarEvent\u001b[0m\u001b[1m(\u001b[0m\u001b[33mname\u001b[0m=\u001b[32m'Dinner with Taylor'\u001b[0m, \u001b[33mdate\u001b[0m=\u001b[32m'Tuesday'\u001b[0m, \u001b[33mparticipants\u001b[0m=\u001b[1m[\u001b[0m\u001b[32m'Leonardo'\u001b[0m, \u001b[32m'Ivan'\u001b[0m, \u001b[32m'Alex'\u001b[0m, \u001b[32m'Taylor'\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
            ],
            "text/html": [
              "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">CalendarEvent</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">name</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'Dinner with Taylor'</span>, <span style=\"color: #808000; text-decoration-color: #808000\">date</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'Tuesday'</span>, <span style=\"color: #808000; text-decoration-color: #808000\">participants</span>=<span style=\"font-weight: bold\">[</span><span style=\"color: #008000; text-decoration-color: #008000\">'Leonardo'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'Ivan'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'Alex'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'Taylor'</span><span style=\"font-weight: bold\">])</span>\n",
              "</pre>\n"
            ]
          },
          "metadata": {}
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Dinner with Taylor\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Responses API\n",
        "\n",
        "https://platform.openai.com/docs/guides/structured-outputs?api-mode=responses"
      ],
      "metadata": {
        "id": "tCzSosP5K-JR"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "To enforce structued output we can use same `create` function and pass `text` property to send json schema"
      ],
      "metadata": {
        "id": "Q4E9IoxrTzFV"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "response = openai.responses.create(\n",
        "    model=MODEL,\n",
        "    input=[\n",
        "        {\"role\": \"developer\", \"content\": \"Extract the event information. \"},\n",
        "        {\"role\": \"user\", \"content\": \"Leonardo, Ivan and Alex will be joining Taylor for dinner on Tuesday night.\"},\n",
        "    ],\n",
        "    # text property is different in chat and responses api\n",
        "    text={\n",
        "        # schema format is also bit different in chat and responses api\n",
        "        \"format\": {\n",
        "            \"type\": \"json_schema\",\n",
        "            \"name\": \"calendar_event\",\n",
        "            \"schema\": {\n",
        "                \"type\": \"object\",\n",
        "                \"properties\": {\n",
        "                    \"name\": { \"type\": \"string\"},\n",
        "                    \"date\": { \"type\": \"string\" },\n",
        "                    \"participants\": { \"type\": \"array\", \"items\": { \"type\": \"string\" }},\n",
        "                },\n",
        "                \"required\": [\"name\", \"date\", \"participants\"],\n",
        "                \"additionalProperties\": False\n",
        "            },\n",
        "            \"strict\": True\n",
        "        }\n",
        "    }\n",
        ")\n",
        "print(response.output_text)\n",
        "dictionary = json.loads(response.output_text)\n",
        "print(dictionary[\"name\"])"
      ],
      "metadata": {
        "id": "2IPLSF30K--S",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "02d38288-7ab3-43ec-ffac-55085777f729"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{\"name\":\"Dinner\",\"date\":\"Tuesday night\",\"participants\":[\"Leonardo\",\"Ivan\",\"Alex\",\"Taylor\"]}\n",
            "Dinner\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "In the example below, we use `openai.responses.parse()`, and the property for format is `text_format` instead of `text`."
      ],
      "metadata": {
        "id": "MfKM-UYGSu_x"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "https://github.com/openai/openai-python/blob/main/examples/responses/structured_outputs.py"
      ],
      "metadata": {
        "id": "n3zCZ8VlT666"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "class CalendarEvent(BaseModel):\n",
        "    name: str\n",
        "    date: str\n",
        "    participants: list[str]\n",
        "\n",
        "response = openai.responses.parse(\n",
        "    model=MODEL,\n",
        "    input=[\n",
        "        {\"role\": \"developer\", \"content\": \"Extract the event information. \"},\n",
        "        {\"role\": \"user\", \"content\": \"Leonardo, Ivan and Alex will be joining Taylor for dinner on Tuesday night.\"},\n",
        "    ],\n",
        "    text_format=CalendarEvent\n",
        ")\n",
        "print(response.output_text)\n",
        "dictionary = json.loads(response.output_text)\n",
        "print(dictionary[\"name\"])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "sejIL04-WVCe",
        "outputId": "80b05d63-0ded-4bd1-da74-311095ab58da"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{\"name\":\"Dinner with Taylor\",\"date\":\"Tuesday night\",\"participants\":[\"Leonardo\",\"Ivan\",\"Alex\",\"Taylor\"]}\n",
            "Dinner with Taylor\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Similar example, execpt for handling parsed object in response"
      ],
      "metadata": {
        "id": "V7_CaKvNUDaL"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Note: `response.output[0].content[0].parsed` directly returns object"
      ],
      "metadata": {
        "id": "JC-wb656UFOQ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "class CalendarEvent(BaseModel):\n",
        "    name: str\n",
        "    date: str\n",
        "    participants: list[str]\n",
        "\n",
        "response = openai.responses.parse(\n",
        "    model=MODEL,\n",
        "    input=[\n",
        "        {\"role\": \"developer\", \"content\": \"Extract the event information. \"},\n",
        "        {\"role\": \"user\", \"content\": \"Leonardo, Ivan and Alex will be joining Taylor for dinner on Tuesday night.\"},\n",
        "    ],\n",
        "    text_format=CalendarEvent\n",
        ")\n",
        "print(response.output_text)\n",
        "print()\n",
        "print(response)\n",
        "print()\n",
        "print(response.output[0].content[0].parsed)\n",
        "rich.print(response.output[0].content[0].parsed)\n",
        "print()\n",
        "obj = response.output[0].content[0].parsed\n",
        "print(obj.date)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 174
        },
        "id": "DrOryISpUDrt",
        "outputId": "d77db324-fe20-42e5-d786-f1749a216519"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{\"name\":\"Dinner with Taylor\",\"date\":\"Tuesday night\",\"participants\":[\"Leonardo\",\"Ivan\",\"Alex\",\"Taylor\"]}\n",
            "\n",
            "ParsedResponse[CalendarEvent](id='resp_67e221956a8481929e2024a06fe7f50808ba6f0314032149', created_at=1742872981.0, error=None, incomplete_details=None, instructions=None, metadata={}, model='gpt-4o-mini-2024-07-18', object='response', output=[ParsedResponseOutputMessage[CalendarEvent](id='msg_67e22195e288819284343194b80e829008ba6f0314032149', content=[ParsedResponseOutputText[CalendarEvent](annotations=[], text='{\"name\":\"Dinner with Taylor\",\"date\":\"Tuesday night\",\"participants\":[\"Leonardo\",\"Ivan\",\"Alex\",\"Taylor\"]}', type='output_text', parsed=CalendarEvent(name='Dinner with Taylor', date='Tuesday night', participants=['Leonardo', 'Ivan', 'Alex', 'Taylor']))], role='assistant', status='completed', type='message')], parallel_tool_calls=True, temperature=1.0, tool_choice='auto', tools=[], top_p=1.0, max_output_tokens=None, previous_response_id=None, reasoning=Reasoning(effort=None, generate_summary=None), status='completed', text=ResponseTextConfig(format=ResponseFormatTextJSONSchemaConfig(schema_={'properties': {'name': {'title': 'Name', 'type': 'string'}, 'date': {'title': 'Date', 'type': 'string'}, 'participants': {'items': {'type': 'string'}, 'title': 'Participants', 'type': 'array'}}, 'required': ['name', 'date', 'participants'], 'title': 'CalendarEvent', 'type': 'object', 'additionalProperties': False}, type='json_schema', description=None, name='CalendarEvent', strict=True)), truncation='disabled', usage=ResponseUsage(input_tokens=112, input_tokens_details=InputTokensDetails(cached_tokens=0), output_tokens=25, output_tokens_details=OutputTokensDetails(reasoning_tokens=0), total_tokens=137), user=None, store=True)\n",
            "\n",
            "name='Dinner with Taylor' date='Tuesday night' participants=['Leonardo', 'Ivan', 'Alex', 'Taylor']\n"
          ]
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "\u001b[1;35mCalendarEvent\u001b[0m\u001b[1m(\u001b[0m\u001b[33mname\u001b[0m=\u001b[32m'Dinner with Taylor'\u001b[0m, \u001b[33mdate\u001b[0m=\u001b[32m'Tuesday night'\u001b[0m, \u001b[33mparticipants\u001b[0m=\u001b[1m[\u001b[0m\u001b[32m'Leonardo'\u001b[0m, \u001b[32m'Ivan'\u001b[0m, \u001b[32m'Alex'\u001b[0m, \u001b[32m'Taylor'\u001b[0m\u001b[1m]\u001b[0m\u001b[1m)\u001b[0m\n"
            ],
            "text/html": [
              "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">CalendarEvent</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">name</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'Dinner with Taylor'</span>, <span style=\"color: #808000; text-decoration-color: #808000\">date</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'Tuesday night'</span>, <span style=\"color: #808000; text-decoration-color: #808000\">participants</span>=<span style=\"font-weight: bold\">[</span><span style=\"color: #008000; text-decoration-color: #008000\">'Leonardo'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'Ivan'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'Alex'</span>, <span style=\"color: #008000; text-decoration-color: #008000\">'Taylor'</span><span style=\"font-weight: bold\">])</span>\n",
              "</pre>\n"
            ]
          },
          "metadata": {}
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\n",
            "Tuesday night\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "You can ask the model to output an answer in a structured, step-by-step way, to guide the user through the solution."
      ],
      "metadata": {
        "id": "dTr7LiaJUJiN"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "class Step(BaseModel):\n",
        "    explanation: str\n",
        "    output: str\n",
        "\n",
        "\n",
        "class MathResponse(BaseModel):\n",
        "    steps: List[Step]\n",
        "    final_answer: str\n",
        "\n",
        "response = openai.responses.parse(\n",
        "    model=MODEL,\n",
        "    input=[\n",
        "        {\"role\": \"developer\", \"content\": \"You are a helpful math tutor. Guide the user through the solution step by step.\"},\n",
        "        {\"role\": \"user\", \"content\": \"how can I solve 8x + 7 = -23\"},\n",
        "    ],\n",
        "    text_format=MathResponse\n",
        ")\n",
        "\n",
        "math_reasoning = response.output[0].content[0].parsed\n",
        "print(math_reasoning)\n",
        "print()\n",
        "print(math_reasoning.final_answer)\n",
        "print()\n",
        "rich.print(math_reasoning)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 266
        },
        "id": "J1aR96vYUJ1Y",
        "outputId": "b1bd10bd-6330-4ed2-fa3e-91cf783e6ce3"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "steps=[Step(explanation='Start with the equation 8x + 7 = -23.', output='8x + 7 = -23'), Step(explanation='Subtract 7 from both sides to isolate the term with x.', output='8x = -23 - 7'), Step(explanation='Calculate -23 - 7 to simplify the equation.', output='8x = -30'), Step(explanation='Now, divide both sides by 8 to solve for x.', output='x = -30 / 8'), Step(explanation='Simplify -30 / 8 to its simplest form.', output='x = -15 / 4 or x = -3.75')] final_answer='x = -15/4 or x = -3.75'\n",
            "\n",
            "x = -15/4 or x = -3.75\n",
            "\n"
          ]
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "\u001b[1;35mMathResponse\u001b[0m\u001b[1m(\u001b[0m\n",
              "    \u001b[33msteps\u001b[0m=\u001b[1m[\u001b[0m\n",
              "        \u001b[1;35mStep\u001b[0m\u001b[1m(\u001b[0m\u001b[33mexplanation\u001b[0m=\u001b[32m'Start with the equation 8x + 7 = -23.'\u001b[0m, \u001b[33moutput\u001b[0m=\u001b[32m'8x + 7 = -23'\u001b[0m\u001b[1m)\u001b[0m,\n",
              "        \u001b[1;35mStep\u001b[0m\u001b[1m(\u001b[0m\u001b[33mexplanation\u001b[0m=\u001b[32m'Subtract 7 from both sides to isolate the term with x.'\u001b[0m, \u001b[33moutput\u001b[0m=\u001b[32m'8x = -23 - 7'\u001b[0m\u001b[1m)\u001b[0m,\n",
              "        \u001b[1;35mStep\u001b[0m\u001b[1m(\u001b[0m\u001b[33mexplanation\u001b[0m=\u001b[32m'Calculate -23 - 7 to simplify the equation.'\u001b[0m, \u001b[33moutput\u001b[0m=\u001b[32m'8x = -30'\u001b[0m\u001b[1m)\u001b[0m,\n",
              "        \u001b[1;35mStep\u001b[0m\u001b[1m(\u001b[0m\u001b[33mexplanation\u001b[0m=\u001b[32m'Now, divide both sides by 8 to solve for x.'\u001b[0m, \u001b[33moutput\u001b[0m=\u001b[32m'x = -30 / 8'\u001b[0m\u001b[1m)\u001b[0m,\n",
              "        \u001b[1;35mStep\u001b[0m\u001b[1m(\u001b[0m\u001b[33mexplanation\u001b[0m=\u001b[32m'Simplify -30 / 8 to its simplest form.'\u001b[0m, \u001b[33moutput\u001b[0m=\u001b[32m'x = -15 / 4 or x = -3.75'\u001b[0m\u001b[1m)\u001b[0m\n",
              "    \u001b[1m]\u001b[0m,\n",
              "    \u001b[33mfinal_answer\u001b[0m=\u001b[32m'x = -15/4 or x = -3.75'\u001b[0m\n",
              "\u001b[1m)\u001b[0m\n"
            ],
            "text/html": [
              "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">MathResponse</span><span style=\"font-weight: bold\">(</span>\n",
              "    <span style=\"color: #808000; text-decoration-color: #808000\">steps</span>=<span style=\"font-weight: bold\">[</span>\n",
              "        <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Step</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">explanation</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'Start with the equation 8x + 7 = -23.'</span>, <span style=\"color: #808000; text-decoration-color: #808000\">output</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'8x + 7 = -23'</span><span style=\"font-weight: bold\">)</span>,\n",
              "        <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Step</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">explanation</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'Subtract 7 from both sides to isolate the term with x.'</span>, <span style=\"color: #808000; text-decoration-color: #808000\">output</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'8x = -23 - 7'</span><span style=\"font-weight: bold\">)</span>,\n",
              "        <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Step</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">explanation</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'Calculate -23 - 7 to simplify the equation.'</span>, <span style=\"color: #808000; text-decoration-color: #808000\">output</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'8x = -30'</span><span style=\"font-weight: bold\">)</span>,\n",
              "        <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Step</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">explanation</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'Now, divide both sides by 8 to solve for x.'</span>, <span style=\"color: #808000; text-decoration-color: #808000\">output</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'x = -30 / 8'</span><span style=\"font-weight: bold\">)</span>,\n",
              "        <span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">Step</span><span style=\"font-weight: bold\">(</span><span style=\"color: #808000; text-decoration-color: #808000\">explanation</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'Simplify -30 / 8 to its simplest form.'</span>, <span style=\"color: #808000; text-decoration-color: #808000\">output</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'x = -15 / 4 or x = -3.75'</span><span style=\"font-weight: bold\">)</span>\n",
              "    <span style=\"font-weight: bold\">]</span>,\n",
              "    <span style=\"color: #808000; text-decoration-color: #808000\">final_answer</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'x = -15/4 or x = -3.75'</span>\n",
              "<span style=\"font-weight: bold\">)</span>\n",
              "</pre>\n"
            ]
          },
          "metadata": {}
        }
      ]
    }
  ]
}